package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.ADDeleteBO;
import com.cskaoyan.bean.bo.ADParam;
import com.cskaoyan.bean.pojo.MallAd;
import com.cskaoyan.bean.vo.AdvertisementListVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AdvertisementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author JJ
 * @description 广告管理
 * @time 2021/7/8 21:55
 */

@RestController
@RequestMapping("admin/ad")
public class AdvertisementController {

    @Autowired
    AdvertisementService advertisementService;

    @RequestMapping("list")
    @RequiresPermissions("admin:ad:list")
    public BaseRespVo list(ADParam param) {


        BaseData<AdvertisementListVo> advertisementListVoBaseData = advertisementService.listAdvertisement(param);

        return BaseRespVo.ok(advertisementListVoBaseData);
    }


   @RequestMapping("create")
   @RequiresPermissions("admin:ad:create")
    public BaseRespVo create(@RequestBody MallAd param) {

       Integer i = advertisementService.createAdvertisement(param);
       if (i<=0){
           return BaseRespVo.fail();
       }
       List<MallAd> mallAds = advertisementService.queryAdvertisement(param);
       //获取唯一的一个商品信息
       MallAd mallAd = mallAds.get(0);

       return BaseRespVo.ok(mallAd);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:ad:update")
    public BaseRespVo update(@RequestBody MallAd param) {

        Integer i = advertisementService.updateAdvertisement(param);
        if (i<=0){
            return BaseRespVo.fail();
        }
        List<MallAd> mallAds = advertisementService.queryAdvertisement(param);
        MallAd mallAd = mallAds.get(0);

        return BaseRespVo.ok(mallAd);
    }



    @RequestMapping("delete")
    @RequiresPermissions("admin:ad:delete")
    public BaseRespVo delete(@RequestBody ADDeleteBO param) {

        Integer i = advertisementService.deleteAD(param);
        if (i<=0){
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok();
    }


}
